package org.itboys.shop.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.portal.entity.Feedback;
import org.itboys.portal.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 *用户反馈
 */
@RestController
@RequestMapping("/wx/feedback/")
public class WXFeedBackController extends BaseController {

	@Autowired
	private FeedbackService feedbackService;
	/**
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toList")
	public ModelAndView toList(Integer page, Integer rows,	Model model, HttpServletRequest request, HttpServletResponse response) {
		if (page == null) {
			page = 1;
		}
		if (rows == null) {
			rows = 10;
		}
		int index = 0;
		model.addAttribute("index", index);
		model.addAttribute("page", page);
		model.addAttribute("rows", rows);
		return new ModelAndView("weixin/feedback/list");
	}	
	
	/**
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/list")
	public void list(
			@RequestParam(value = "page", required = false) Integer page, 
			@RequestParam(value = "rows", required = false) Integer rows,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Feedback> list = new ArrayList<Feedback>();
//			Map<String, Object> map = new HashMap<String, Object>();
			if (page == null) {
				page = 1;
			}
			if (rows == null) {
				rows = 10;
			}
			JsonPageUtils.renderJsonPage(0L, list, response);
		} catch (Exception e) {
			logger.error("product list error!", e);
			AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(-1, "系统错误"));
		}
	}
	
	/**
	 * 编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toEdit")
	public ModelAndView toEdit(Feedback bean,Model model){
		model.addAttribute("bean", bean);
		addAttribute(model);
		return new ModelAndView("member/feedback");
	}
	/**
	 * 
	 * @param bean
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/add")
	public void add(Feedback bean, HttpServletRequest request, HttpServletResponse response) {
		try {
			Member member = (Member) request.getSession().getAttribute("user");
			Long memberId = member.getId();
			bean.setMemberId(memberId);
			bean.setName(member.getUsername());
			bean.setCreateTime(new Date());
			bean.setStatus(1);
			feedbackService.saveWithoutLogin(bean);
			AjaxUtils.renderJson(response, ResultPageDOHelper.getMsgResultDO(null, "操作成功"));
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJson(response, ResultPageDOHelper.getErrorResultDO(-1, "系统异常!"));
		}
	}
}
